<?php
/**
 +------------------------------------------------------------------------------
 * PicAction控制器类
 +------------------------------------------------------------------------------
 * @category   SubAction
 * @package  app
 * @subpackage  Action
 * @author   朝闻道 <hydata@gmail.com>
 * @date 2011-3-6
 * @time  下午04:51:30
 +------------------------------------------------------------------------------
 */
class PicAction extends Action {
	/**
	 * 根据唯一码获取图片
	 * img 图片唯一码 
	 * y 图片水印
	 * mod 图片大小模式
	 */
	public function index() {
		$img = explode ( '.', $_GET ['img'] );
		$img = $img [0];
		$dao = M ( "Pic" );
		$im=array();
		$im = $dao->where ( "short='$img'" )->field ( "id,path,images,view,small" )->find ();
		$imgFile = $im ['path'] . $im ['images'];
		$y = explode ( '.', $_GET ['y'] );
		$y = $y [0];
		if ($y != 'iicc') {
			$text = "和政文化传播有限公司";
			$text2 = "www.hezheng.cc";
		}
		import ( "ORG.Util.Image" );
		if ($_GET ['mod'] == 'small') {
			self::small ( $imgFile, 80 );
			$dao->where("id='{$im['id']}'")->setField ( array("view","small"), array($im['view']+1,$im['small']+1) );
		}elseif ($_GET ['mod'] == 'mid'){
			self::small ( $imgFile, 300 );
			$dao->where("id='{$im['id']}'")->setField ( array("view","mid"), array($im['view']+1,$im['mid']+1) );
		} else {
			self::show ( $imgFile, $text, $text2 );
			$dao->setInc ( "view", "id='{$im['id']}'" );
		}
	}
	
	/**
     +----------------------------------------------------------
	 * 显示原图
	 * @date 2011-3-8 - @time 上午10:38:11
     +----------------------------------------------------------
	 * @static
	 * @access public
     +----------------------------------------------------------
	 * @param string $imgFile
	 * @param string $text
     +----------------------------------------------------------
	 * @return void
     +----------------------------------------------------------
	 */
	function show($imgFile, $text = '', $text2 = '') {
		//显示原图
		$info = Image::getImageInfo ( $imgFile );
		if ($info !== false) {
			$createFun = str_replace ( '/', 'createfrom', $info ['mime'] );
			$im = $createFun ( $imgFile );
			if ($im) {
				$ImageFun = str_replace ( '/', '', $info ['mime'] );
				if (! empty ( $text )) {
					$tc = imagecolorallocate ( $im, 0, 0, 0 );
					//imagestring ( $im, 5, 5, 5, $text, $tc );
					imagettftext ( $im, 12, 0, 5, 20, $tc, './Public/font/simsun.ttc', $text );
					if ($info ['width'] > 260 && $info ['height'] > 40) {
						imagettftext ( $im, 12, 0, $info ['width'] - 126, $info ['height'] - 35, $tc, './Public/font/simsun.ttc', $text );
					}
					if ($text2) {
						imagettftext ( $im, 12, 0, 5, 40, $tc, './Public/font/simsun.ttc', $text2 );
						if ($info ['width'] > 260 && $info ['height'] > 100) {
							imagettftext ( $im, 12, 0, $info ['width'] - 126, $info ['height'] - 15, $tc, './Public/font/simsun.ttc', $text2 );
						}
					}
				}
				if ($info ['type'] == 'png' || $info ['type'] == 'gif') {
					imagealphablending ( $im, false ); //取消默认的混色模式
					imagesavealpha ( $im, true ); //设定保存完整的 alpha 通道信息
				}
				header ( "Content-type: " . $info ['mime'] );
				$ImageFun ( $im );
				imagedestroy ( $im );
				return;
			}
		}
		//获取或者创建图像文件失败则生成空白PNG图片
		$im = imagecreatetruecolor ( 200, 200 );
		$bgc = imagecolorallocate ( $im, 255, 255, 255 );
		$tc = imagecolorallocate ( $im, 0, 0, 0 );
		imagefilledrectangle ( $im, 0, 0, 150, 30, $bgc );
		imagestring ( $im, 4, 5, 5, "NO PIC", $tc );
		Image::output ( $im );
		return;
	} //end show
	

	/**
	 +----------------------------------------------------------
	 * 给出缩略图
	 * @date 2011-3-8 - @time 上午11:25:50
	 +----------------------------------------------------------
	 * @static
	 * @access public
	 +----------------------------------------------------------
	 * @param string 
	 +----------------------------------------------------------
	 * @return void
	 +----------------------------------------------------------
	 */
	function small($imgFile, $w = 0, $h = 0) {
		//给出缩略图
		$info = Image::getImageInfo ( $imgFile );
		if ($info !== false) {
			if ($w != 0) {
				$wbl = $w / $info ['width'];
				$h = $info ['height'] * $wbl;
			}else{
				$hbl = $h / $info ['height'];
				$w = $info ['width'] * $hbl / 100;
			}
			
			$createFun = str_replace ( '/', 'createfrom', $info ['mime'] );
			$im = $createFun ( $imgFile );
			$new_im = imagecreatetruecolor ( $w, $h );
			
			ImageCopyResampled ( $new_im, $im, 0, 0, 0, 0, $w, $h, $info ['width'], $info ['height'] );
			if ($new_im) {
				$ImageFun = str_replace ( '/', '', $info ['mime'] );
				if ($info ['type'] == 'png' || $info ['type'] == 'gif') {
					imagealphablending ( $new_im, false ); //取消默认的混色模式
					imagesavealpha ( $new_im, true ); //设定保存完整的 alpha 通道信息
				}
				header ( "Content-type: " . $info ['mime'] );
				$ImageFun ( $new_im );
				imagedestroy ( $new_im );
				return;
			}
		}
		
		//获取或者创建图像文件失败则生成空白PNG图片
		$im = imagecreatetruecolor ( 100, 100 );
		$bgc = imagecolorallocate ( $im, 255, 255, 255 );
		$tc = imagecolorallocate ( $im, 0, 0, 0 );
		imagefilledrectangle ( $im, 0, 0, 150, 30, $bgc );
		imagestring ( $im, 4, 5, 5, "NO PIC", $tc );
		Image::output ( $im );
		return;
	} //end small
	
	/**
	   *下载指定文件
	   *@date 2011-3-25
	   *@time 上午11:20:34
	   */
	function dl() {
		//下载指定文件
		//dump($_GET);
		$img = explode ( '.', $_GET ['hash'] );
		$img = $img [0];
		
		$dao = M ( "Pic" );
		$im=array();
		$im = $dao->where ( "short='$img'" )->field ( "id,path_url,images,view,filetype" )->find ();
		//dump($dao->getlastsql());
		if($im){
			$imgFile = $im ['path_url'] . $im ['images'];
			$dao->where("id='{$im['id']}'")->setField ( array("view"), array($im['view']+1) );
			Header( "HTTP/1.1 301 Moved Permanently" );
			Header( "Location:http://{$_SERVER["HTTP_HOST"]}.$imgFile");
		}else{
			$this->assign("jumpUrl",__APP__);
			$this->error("文件不存在，或者已经被删除。");
		}
	}//end dl
}//end PicAction